home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_105 / bison / lr0.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  14KB  |  658 lines

  1. /* Generate the nondeterministic finite state machine for bison,
  2.    copyright (C) 1984 Bob Corbett and Richard Stallman
  3.  
  4.    Permission is granted to anyone to make or distribute verbatim copies of this program
  5.    provided that the copyright notice and this permission notice are preserved;
  6.    and provided that the recipient is not asked to waive or limit his right to
  7.    redistribute copies as permitted by this permission notice;
  8.    and provided that anyone possessing an executable copy
  9.    is granted access to copy the source code, in machine-readable form,
  10.    in some reasonable manner.
  11.  
  12.    Permission is granted to distribute derived works or enhanced versions of
  13.    this program under the above conditions with the additional condition
  14.    that the entire derivative or enhanced work
  15.    must be covered by a permission notice identical to this one.
  16.  
  17.    Anything distributed as part of a package containing portions derived
  18.    from this program, which cannot in current practice perform its function usefully
  19.    in the absense of what was derived directly from this program,
  20.    is to be considered as forming, together with the latter,
  21.    a single work derived from this program,
  22.    which must be entirely covered by a permission notice identical to this one
  23.    in order for distribution of the package to be permitted.
  24.  
  25.  In other words, you are welcome to use, share and improve this program.
  26.  You are forbidden to forbid anyone else to use, share and improve
  27.  what you give them.   Help stamp out software-hoarding!  */
  28.  
  29. /* See comments in state.h for the data structures that represent it.
  30.    The entry point is generate_states.  */
  31.  
  32. #include <stdio.h>
  33. #include "machine.h"
  34. #include "new.h"
  35. #include "gram.h"
  36. #include "state.h"
  37.  
  38.  
  39. extern short *itemset;
  40. extern short *itemsetend;
  41.  
  42.  
  43. int nstates;
  44. int final_state;
  45. core *first_state;
  46. shifts *first_shift;
  47. reductions *first_reduction;
  48.  
  49. int get_state();
  50. core *new_state();
  51.  
  52. static core *this_state;
  53. static core *last_state;
  54. static shifts *last_shift;
  55. static reductions *last_reduction;
  56.  
  57. static int nshifts;
  58. static short *shift_symbol;
  59.  
  60. static short *redset;
  61. static short *shiftset;
  62.  
  63. static short **kernel_base;
  64. static short **kernel_end;
  65. static short *kernel_items;
  66.  
  67. /* hash table for states, to recognize equivalent ones.  */
  68.  
  69. #define    STATE_TABLE_SIZE    1009
  70. static core **state_table;
  71.  
  72.  
  73.  
  74. allocate_itemsets()
  75. {
  76.   register short *itemp;
  77.   register int symbol;
  78.   register int i;
  79.   register count;
  80.   register int maxcount;
  81.   register short *symbol_count;
  82.  
  83.   count = 0;
  84.   symbol_count = NEW2(nsyms, short);
  85.  
  86.   itemp = ritem;
  87.   symbol = *itemp++;
  88.   while (symbol)
  89.     {
  90.       if (symbol > 0)
  91.     {
  92.       count++;
  93.       symbol_count[symbol]++;
  94.     }
  95.       symbol = *itemp++;
  96.     }
  97.  
  98.   /* see comments before new-itemset.  All the vectors of items
  99.      live inside kernel_items.  The number of active items after
  100.      some symbol cannot be more than the number of times that symbol
  101.      appears as an item, which is symbol_count[symbol].
  102.      We allocate that much space for each symbol.  */
  103.  
  104.   kernel_base = NEW2(nsyms, short *);
  105.   kernel_items = NEW2(count, short);
  106.  
  107.   count = 0;
  108.   maxcount = 0;
  109.   for (i = 0; i < nsyms; i++)
  110.     {
  111.       kernel_base[i] = kernel_items + count;
  112.       count += symbol_count[i];
  113.       if (maxcount < symbol_count[i])
  114.     maxcount = symbol_count[i];
  115.     }
  116.  
  117.   shift_symbol = symbol_count;
  118.   kernel_end = NEW2(nsyms, short *);
  119. }
  120.  
  121.  
  122.  
  123. allocate_storage()
  124. {
  125.   allocate_itemsets();
  126.  
  127.   shiftset = NEW2(nsyms, short);
  128.   redset = NEW2(nrules + 1, short);
  129.   state_table = NEW2(STATE_TABLE_SIZE, core *);
  130. }
  131.  
  132.  
  133.  
  134. free_storage()
  135. {
  136.   FREE(shift_symbol);
  137.   FREE(redset);
  138.   FREE(shiftset);
  139.   FREE(kernel_base);
  140.   FREE(kernel_end);
  141.   FREE(kernel_items);
  142.   FREE(state_table);
  143. }
  144.  
  145.  
  146.  
  147. /* compute the nondeterministic finite state machine (see state.h for details)
  148. from the grammar.  */
  149.  
  150. generate_states()
  151. {
  152.   allocate_storage();
  153.   initialize_closure(nitems);
  154.   initialize_states();
  155.  
  156.   while (this_state)
  157.     {
  158.       /* Set up ruleset and itemset for the transitions out of this state.
  159.          ruleset gets a 1 bit for each rule that could reduce now.
  160.      itemset gets a vector of all the items that could be accepted next.  */
  161.       closure(this_state->items, this_state->nitems);
  162.       /* record the reductions allowed out of this state */
  163.       save_reductions();
  164.       /* find the itemsets of the states that shifts can reach */
  165.       new_itemsets();
  166.       /* find or create the core structures for those states */
  167.       append_states();
  168.  
  169.       /* create the shifts structures for the shifts to those states,
  170.          now that the state numbers transitioning to are known */
  171.       if (nshifts > 0)
  172.         save_shifts();
  173.  
  174.       /* states are queued when they are created; process them all */
  175.       this_state = this_state->next;
  176.     }
  177.  
  178.   /* discard various storage */
  179.   finalize_closure();
  180.   free_storage();
  181.  
  182.   /* set up initial and final states as parser wants them */
  183.   augment_automaton();
  184. }
  185.  
  186.  
  187.  
  188. /* Find which symbols can be shifted in the current state,
  189.    and for each one record which items would be active after that shift.
  190.    Uses the contents of itemset.
  191.    shift_symbol is set to a vector of the symbols that can be shifted.
  192.    For each symbol in the grammer, kernel_base[symbol] points to
  193.    a vector of item numbers activated if that symbol is shifted,
  194.    and kernel_end[symbol] points after the end of that vector.  */
  195.  
  196. new_itemsets()
  197. {
  198.   register int i;
  199.   register int shiftcount;
  200.   register short *isp;
  201.   register short *ksp;
  202.   register int symbol;
  203.  
  204. #ifdef    TRACE
  205.   fprintf(stderr, "Entering new_itemsets\n");
  206. #endif
  207.  
  208.   for (i = 0; i < nsyms; i++)
  209.     kernel_end[i] = NULL;
  210.  
  211.   shiftcount = 0;
  212.  
  213.   isp = itemset;
  214.  
  215.   while (isp < itemsetend)
  216.     {
  217.       i = *isp++;
  218.       symbol = ritem[i];
  219.       if (symbol > 0)
  220.     {
  221.           ksp = kernel_end[symbol];
  222.  
  223.           if (!ksp)
  224.         {
  225.           shift_symbol[shiftcount++] = symbol;
  226.           ksp = kernel_base[symbol];
  227.         }
  228.  
  229.           *ksp++ = i + 1;
  230.           kernel_end[symbol] = ksp;
  231.     }
  232.     }
  233.  
  234.   nshifts = shiftcount;
  235. }
  236.  
  237.  
  238.  
  239. /* Use the information computed by new_itemset to find the state numbers
  240.    reached by each shift transition from the current state.
  241.  
  242.    shiftset is set up as a vector of state numbers of those states.  */
  243.  
  244. append_states()
  245. {
  246.   register int i;
  247.   register int j;
  248.   register int symbol;
  249.  
  250. #ifdef    TRACE
  251.   fprintf(stderr, "Entering append_states\n");
  252. #endif
  253.  
  254.   /* first sort shift_symbol into increasing order */
  255.  
  256.   for (i = 1; i < nshifts; i++)
  257.     {
  258.       symbol = shift_symbol[i];
  259.       j = i;
  260.       while (j > 0 && shift_symbol[j - 1] > symbol)
  261.     {
  262.       shift_symbol[j] = shift_symbol[j - 1];
  263.       j--;
  264.     }
  265.       shift_symbol[j] = symbol;
  266.     }
  267.  
  268.   for (i = 0; i < nshifts; i++)
  269.     {
  270.       symbol = shift_symbol[i];
  271.       shiftset[i] = get_state(symbol);
  272.     }
  273. }
  274.  
  275.  
  276.  
  277. /* find the state number for the state we would get to
  278. (from the current state) by shifting symbol.
  279. Create a new state if no equivalent one exists already.
  280. Used by append_states  */
  281.  
  282. int
  283. get_state(symbol)
  284. int symbol;
  285. {
  286.   register int key;
  287.   register short *isp1;
  288.   register short *isp2;
  289.   register short *iend;
  290.   register core *sp;
  291.   register int found;
  292.  
  293.   int n;
  294.  
  295. #ifdef    TRACE
  296.   fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  297. #endif
  298.  
  299.   isp1 = kernel_base[symbol];
  300.   iend = kernel_end[symbol];
  301.   n = iend - isp1;
  302.  
  303.   /* add up the target state's active item numbers to get a hash key */
  304.   key = 0;
  305.   while (isp1 < iend)
  306.     key += *isp1++;
  307.  
  308.   key = key % STATE_TABLE_SIZE;
  309.  
  310.   sp = state_table[key];
  311.  
  312.   if (sp)
  313.     {
  314.       found = 0;
  315.       while (!found)
  316.     {
  317.       if (sp->nitems == n)
  318.         {
  319.           found = 1;
  320.           isp1 = kernel_base[symbol];
  321.           isp2 = sp->items;
  322.  
  323.           while (found && isp1 < iend)
  324.         {
  325.           if (*isp1++ != *isp2++)
  326.             found = 0;
  327.         }
  328.         }
  329.  
  330.       if (!found)
  331.         {
  332.           if (sp->link)
  333.         {
  334.           sp = sp->link;
  335.         }
  336.           else   /* bucket exhausted and no match */
  337.         {
  338.           sp = sp->link = new_state(symbol);
  339.           found = 1;
  340.         }
  341.         }
  342.     }
  343.     }
  344.   else      /* bucket is empty */
  345.     {
  346.       state_table[key] = sp = new_state(symbol);
  347.     }
  348.  
  349.   return ((int) sp->number);
  350. }
  351.  
  352.  
  353.  
  354. /* subroutine of get_state.  create a new state for those items, if necessary.  */
  355.  
  356. core *
  357. new_state(symbol)
  358. int symbol;
  359. {
  360.   register int n;
  361.   register core *p;
  362.   register short *isp1;
  363.   register short *isp2;
  364.   register short *iend;
  365.  
  366. #ifdef    TRACE
  367.   fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  368. #endif
  369.  
  370.   if (nstates >= MAXSHORT)
  371.     toomany("states");
  372.  
  373.   isp1 = kernel_base[symbol];
  374.   iend = kernel_end[symbol];
  375.   n = iend - isp1;
  376.  
  377.   p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  378.   p->accessing_symbol = symbol;
  379.   p->number = nstates;
  380.   p->nitems = n;
  381.  
  382.   isp2 = p->items;
  383.   while (isp1 < iend)
  384.     *isp2++ = *isp1++;
  385.  
  386.   last_state->next = p;
  387.   last_state = p;
  388.  
  389.   nstates++;
  390.  
  391.   return (p);
  392. }
  393.  
  394.  
  395.  
  396. initialize_states()
  397. {
  398.   register core *p;
  399.  
  400.   p = (core *) allocate((unsigned) (sizeof(core) - sizeof(short)));
  401.   first_state = last_state = this_state = p;
  402.   nstates = 1;
  403. }
  404.  
  405.  
  406.  
  407. save_shifts()
  408. {
  409.   register shifts *p;
  410.   register short *sp1;
  411.   register short *sp2;
  412.   register short *send;
  413.  
  414.   p = (shifts *) allocate((unsigned) (sizeof(shifts) +
  415.             (nshifts - 1) * sizeof(short)));
  416.  
  417.   p->number = this_state->number;
  418.   p->nshifts = nshifts;
  419.  
  420.   sp1 = shiftset;
  421.   sp2 = p->shifts;
  422.   send = shiftset + nshifts;
  423.  
  424.   while (sp1 < send)
  425.     *sp2++ = *sp1++;
  426.  
  427.   if (last_shift)
  428.     {
  429.       last_shift->next = p;
  430.       last_shift = p;
  431.     }
  432.   else
  433.     {
  434.       first_shift = p;
  435.       last_shift = p;
  436.     }
  437. }
  438.  
  439.  
  440.  
  441. /* find which rules can be used for reduction transitions from the current state
  442.    and make a reductions structure for the state to record their rule numbers.  */
  443.  
  444. save_reductions()
  445. {
  446.   register short *isp;
  447.   register short *rp1;
  448.   register short *rp2;
  449.   register int item;
  450.   register int count;
  451.   register reductions *p;
  452.  
  453.   short *rend;
  454.  
  455.   /* find and count the active items that represent ends of rules */
  456.  
  457.   count = 0;
  458.   for (isp = itemset; isp < itemsetend; isp++)
  459.     {
  460.       item = ritem[*isp];
  461.       if (item < 0)
  462.     {
  463.       redset[count++] = -item;
  464.     }
  465.     }
  466.  
  467.   /* make a reductions structure and copy the data into it.  */
  468.  
  469.   if (count)
  470.     {
  471.       p = (reductions *) allocate((unsigned) (sizeof(reductions) +
  472.                     (count - 1) * sizeof(short)));
  473.  
  474.       p->number = this_state->number;
  475.       p->nreds = count;
  476.  
  477.       rp1 = redset;
  478.       rp2 = p->rules;
  479.       rend = rp1 + count;
  480.  
  481.       while (rp1 < rend)
  482.     *rp2++ = *rp1++;
  483.  
  484.       if (last_reduction)
  485.     {
  486.       last_reduction->next = p;
  487.       last_reduction = p;
  488.     }
  489.       else
  490.     {
  491.       first_reduction = p;
  492.       last_reduction = p;
  493.     }
  494.     }
  495. }
  496.  
  497.  
  498.  
  499. /* Make sure that the initial state has a shift that accepts the
  500. grammar's start symbol and goes to the next-to-final state,
  501. which has a shift going to the final state, which has a shift
  502. to the termination state.
  503. Create such states and shifts if they don't happen to exist already.  */
  504.  
  505. augment_automaton()
  506. {
  507.   register int i;
  508.   register int k;
  509.   register core *statep;
  510.   register shifts *sp;
  511.   register shifts *sp2;
  512.   register shifts *sp1;
  513.  
  514.   sp = first_shift;
  515.  
  516.   if (sp)
  517.     {
  518.       if (sp->number == 0)
  519.     {
  520.       k = sp->nshifts;
  521.       statep = first_state->next;
  522.  
  523.       while (statep->accessing_symbol < start_symbol
  524.           && statep->number < k)
  525.         statep = statep->next;
  526.  
  527.       if (statep->accessing_symbol == start_symbol)
  528.         {
  529.           k = statep->number;
  530.  
  531.           while (sp->number < k)
  532.         {
  533.           sp1 = sp;
  534.           sp = sp->next;
  535.         }
  536.  
  537.           if (sp->number == k)
  538.         {
  539.           sp2 = (shifts *) allocate((unsigned) (sizeof(shifts)
  540.                     + sp->nshifts * sizeof(short)));
  541.           sp2->next = sp->next;
  542.           sp2->number = k;
  543.           sp2->nshifts = sp->nshifts + 1;
  544.           sp2->shifts[0] = nstates;
  545.           for (i = sp->nshifts; i > 0; i--)
  546.             sp2->shifts[i] = sp->shifts[i - 1];
  547.  
  548.           sp1->next = sp2;
  549.           FREE(sp);
  550.         }
  551.           else
  552.         {
  553.           sp2 = NEW(shifts);
  554.           sp2->next = sp;
  555.           sp2->number = k;
  556.           sp2->nshifts = 1;
  557.           sp2->shifts[0] = nstates;
  558.  
  559.           sp1->next = sp2;
  560.           if (!sp)
  561.             last_shift = sp2;
  562.         }
  563.         }
  564.       else
  565.         {
  566.           k = statep->number;
  567.           sp = first_shift;
  568.  
  569.           sp2 = (shifts *) allocate((unsigned) (sizeof(shifts)
  570.                     + sp->nshifts * sizeof(short)));
  571.           sp2->next = sp->next;
  572.           sp2->nshifts = sp->nshifts + 1;
  573.  
  574.           for (i = 0; i < k; i++)
  575.         sp2->shifts[i] = sp->shifts[i];
  576.  
  577.           sp2->shifts[k] = nstates;
  578.  
  579.           for (i = k; i < sp->nshifts; i++)
  580.         sp2->shifts[i + 1] = sp->shifts[i];
  581.  
  582.           first_shift = sp2;
  583.           if (last_shift == sp)
  584.         last_shift = sp2;
  585.  
  586.           FREE(sp);
  587.  
  588.           insert_start_shift();
  589.         }
  590.     }
  591.       else
  592.     {
  593.       sp = NEW(shifts);
  594.       sp->next = first_shift;
  595.       sp->nshifts = 1;
  596.       sp->shifts[0] = nstates;
  597.  
  598.       first_shift = sp;
  599.  
  600.       insert_start_shift();
  601.     }
  602.     }
  603.   else
  604.     {
  605.       sp = NEW(shifts);
  606.       sp->nshifts = 1;
  607.       sp->shifts[0] = nstates;
  608.  
  609.       first_shift = sp;
  610.       last_shift = sp;
  611.  
  612.       insert_start_shift();
  613.     }
  614.  
  615.   statep = (core *) allocate((unsigned) (sizeof(core) - sizeof(short)));
  616.   statep->number = nstates;
  617.   last_state->next = statep;
  618.   last_state = statep;
  619.  
  620.   sp = NEW(shifts);
  621.   sp->number = nstates++;
  622.   sp->nshifts = 1;
  623.   sp->shifts[0] = nstates;
  624.   last_shift->next = sp;
  625.   last_shift = sp;
  626.  
  627.   final_state = nstates;
  628.  
  629.   statep = (core *) allocate((unsigned) (sizeof(core) - sizeof(short)));
  630.   statep->number = nstates++;
  631.   last_state->next = statep;
  632.   last_state = statep;
  633. }
  634.  
  635.  
  636. /* subroutine of augment_automaton */
  637.  
  638. insert_start_shift()
  639. {
  640.   register core *statep;
  641.   register shifts *sp;
  642.  
  643.   statep = (core *) allocate((unsigned) (sizeof(core) - sizeof(short)));
  644.   statep->number = nstates;
  645.   statep->accessing_symbol = start_symbol;
  646.  
  647.   last_state->next = statep;
  648.   last_state = statep;
  649.  
  650.   sp = NEW(shifts);
  651.   sp->number = nstates++;
  652.   sp->nshifts = 1;
  653.   sp->shifts[0] = nstates;
  654.  
  655.   last_shift->next = sp;
  656.   last_shift = sp;
  657. }
  658.